package com.sskj.wallet.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.sskj.common.base.App;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.ArithUtil;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.widget.BottomDialog;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.CoinListBean;
import com.sskj.wallet.bean.WithdrawMsgBean;
import com.sskj.wallet.presenter.WithdrawPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @author Hey
 * Create at  2019/06/26
 */
@Route(path = RConfig.WALLET_ACTIVITY_WITHDRAW)
public class WithdrawActivity extends BaseActivity<WithdrawPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.iv_right)
    ImageView ivRight;
    @BindView(R2.id.tv_coin)
    TextView tvCoin;
    @BindView(R2.id.ll_choose_coin)
    LinearLayout llChooseCoin;
    @BindView(R2.id.et_usable)
    EditText etUsable;
    @BindView(R2.id.et_address)
    EditText etAddress;
    @BindView(R2.id.iv_saoma)
    ImageView ivSaoma;
    @BindView(R2.id.iv_address)
    ImageView ivAddress;
    @BindView(R2.id.et_amount)
    EditText etAmount;
    @BindView(R2.id.tv_coin1)
    TextView tvCoin1;
    @BindView(R2.id.tv_all)
    TextView tvAll;
    @BindView(R2.id.tv_daozhangshuliang)
    TextView tvDaozhangshuliang;
    @BindView(R2.id.tv_shouxufei)
    TextView tvShouxufei;
    @BindView(R2.id.btn_withdraw)
    TextView btnWithdraw;

    private List<CoinListBean> coinListBeans = new ArrayList<>();
    List<String> coins = new ArrayList<>();

    private Double fee;//手续费
    private String min;//最小提币金额
    private String max;//最大提币金额
    private String usable;//当前所选币种可用金额

    private String input;

    @Override
    public int getLayoutId() {
        return R.layout.wallet_activity_withdraw;
    }

    @Override
    public WithdrawPresenter getPresenter() {
        return new WithdrawPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.wallet_chongtiDetailsActivity2));
        ivRight.setImageResource(R.mipmap.icon_jilu);
        ivRight.setVisibility(View.VISIBLE);
        etAmount.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(8)});
        setEditListener();
    }

    private void setEditListener() {
        etAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                input = s.toString();
                if (isEmpty(input)) {
                    input = "0";
                }
                if (fee == null) {
                    return;
                }
                tvDaozhangshuliang.setText(App.INSTANCE.getString(R.string.wallet_withdrawActivity2) + CommonUtil.dealCoinNum(String.valueOf(
                        ArithUtil.sub(Double.valueOf(input), ArithUtil.mul(Double.valueOf(input), fee))), getText(tvCoin)));
            }
        });
    }

    @Override
    public void initData() {
        mPresenter.getCoinLists(2);
        //选择币种
        ClickUtil.click(300, llChooseCoin, () -> {
            BottomDialog.chooseCoins(WithdrawActivity.this, coins, tv -> {
                tvCoin.setText(tv);
                tvCoin1.setText(tv);
                etAmount.getText().clear();
                mPresenter.getWithdrawMsg(coinListBeans.get(coins.indexOf(tv)).getPid());
            });
        });
        //查看提币地址列表
        ClickUtil.click(300, ivAddress, () -> {
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_ADDRESS_LIST).navigation(this, Constans.ADDRESS_ID);
        });
        //扫码获取提币地址
        ClickUtil.click(300, ivSaoma, () -> {
            ARouter.getInstance().build(RConfig.QUICK_MARK).navigation(this, Constans.QUCIK_BACK);
        });
        //提币记录
        ClickUtil.click(300, ivRight, () -> {
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_RECORD_CHONGTI)
                    .withString(Constans.RECORD_TYPE, Constans.WITHDRAW).navigation();
        });
        ClickUtil.click(300, btnWithdraw, () -> {
            if (isEmpty(min)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.wallet_withdrawActivity3));
                return;
            }
            if (isEmpty(etAddress)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.wallet_withdrawActivity4));
                return;
            }
            if (isEmpty(etAmount)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.wallet_withdrawActivity5));
                return;
            }
            if (Double.valueOf(getText(etAmount)) < Double.valueOf(min)) {
                toastHint(etAmount);
                return;
            }
            if (Double.valueOf(getText(etAmount)) > Double.valueOf(max)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.wallet_withdrawActivity6) + max);
                return;
            }
            mPresenter.withDraw(getText(etAmount), coinListBeans.get(coins.indexOf(getText(tvCoin))).getPid(), getText(etAddress));

        });
        //全部
        ClickUtil.click(300, tvAll, () -> {
            if (isEmpty(usable)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.wallet_transferActivity7));
                return;
            }
            etAmount.setText(usable);
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, WithdrawActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == Constans.QUCIK_BACK) {
            String scan_result = data.getStringExtra("scan_result");
            etAddress.setText(TextUtils.isEmpty(scan_result) ? "" : scan_result);
        }
        if (resultCode == RESULT_OK && requestCode == Constans.ADDRESS_ID) {
            String addressId = data.getStringExtra("addressId");
            etAddress.setText(TextUtils.isEmpty(addressId) ? "" : addressId);
        }
    }

    public void getCoinListsOK(List<CoinListBean> data) {
        if (data == null || data.size() == 0) {
            return;
        }
        coinListBeans = data;
        Observable.fromIterable(data)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new Observer<CoinListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CoinListBean coinListBean) {
                        coins.add(coinListBean.getPname());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        tvCoin.setText(coins.get(0));
                        tvCoin1.setText(coins.get(0));
                        mPresenter.getWithdrawMsg(coinListBeans.get(0).getPid());
                    }
                });
    }

    public void getWithDrawMsgOK(WithdrawMsgBean data) {
        if (data == null) {
            return;
        }
        fee = Double.valueOf(data.getTb_fee()) / 100;
        min = data.getTb_min();
        max = data.getTb_max();
        usable = data.getUser_usable();
        etUsable.setText(data.getUser_usable() + " " + getText(tvCoin));
        etAmount.setHint(App.INSTANCE.getString(R.string.wallet_withdrawActivity8) + data.getTb_min());
        tvShouxufei.setText(App.INSTANCE.getString(R.string.wallet_withdrawActivity9) + data.getTb_fee() + "%");
    }

    public void withDrawOK() {
        ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_RECORD_CHONGTI)
                .withString(Constans.RECORD_TYPE, Constans.WITHDRAW).navigation();
    }
}
